import { Controller, Get, Param, Req } from '@nestjs/common';
import { PostService } from './post.service';
import { ApiTags } from '@nestjs/swagger';
import { AnalyticsService } from '../analytics/analytics.service';
import { EventType } from '../entities';

@ApiTags('小程序/文章')
@Controller('app/post')
export class PostController {
  constructor(
    private readonly postService: PostService,
    private readonly analyticsService: AnalyticsService,
  ) {}

  /**
   * 文章列表
   */
  @Get()
  postList() {
    return this.postService.findAll();
  }

  /**
   * 文章详情
   */
  @Get(':postId')
  async postDetail(@Req() req: Request, @Param('postId') postId: number) {

    if(req.user){
      await this.analyticsService.create(req.user, EventType.NUTRITION_EDUCATION);
    }

    return this.postService.findOne(postId);
  }
}
